package com.app.asktom;

import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.app.asktom.abs.AbsActivity;
import com.app.asktom.abs.AbsHttpAsync;
import com.app.asktom.component.Config;
import com.app.asktom.component.HttpHelper;
import com.app.asktom.component.JsonHelper;
import com.app.asktom.model.User;

public class ActivationActivity extends AbsActivity {

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activation);
		// Show the Up button in the action bar.
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_activation, menu);
		return true;
	}
	
	public void activateAccount(View view) {
//		this.disableButton(R.id.btnActivationBack,R.id.btnActivate);
		ActivationTask activationTask = new ActivationTask();
		activationTask.setShowLoading(true);
		activationTask.setActivity(this);
		activationTask.execute(Config.getInstance().get("baseUrl"));
		this.hideKeyboard(R.id.txtActivationCode);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private class ActivationTask extends AbsHttpAsync {

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			if(this.getResponse() == null || this.getResponse().length() == 0) {
				Toast.makeText(this.activity, "Please try again", 
						Toast.LENGTH_LONG).show();
				this.dialog.dismiss();
				return;
			}
			int start = this.getResponse().indexOf("{");
			int end = this.getResponse().length();
			String msg = "failed";
			Log.d("DEBUG", this.getResponse());
			if(this.getResponse().startsWith("error:")) {
				
			} else if(start >= 0) {
				this.response = this.response.substring(start, end);
				Map<String,String> json = JsonHelper.getInstance()
						.responseActivation(this.response);
				if(json.get("stat").equals("OK")) {
					User user = new User(this.activity);
					user.updateUserSetActivated(
						this.getTextValue(R.id.txtActivationCode));		
					msg = json.get("msg");
					Toast.makeText(this.activity, msg,
							Toast.LENGTH_LONG).show();
					this.dialog.dismiss();
					Intent intent = new Intent(this.activity,
						ViewFoodActivity.class);
					this.activity.startActivity(intent);
					this.activity.finish();

				}
				msg = json.get("msg");
			}
			Toast.makeText(this.activity, msg, Toast.LENGTH_LONG)
				.show();
			super.onPostExecute(result);
		}
		
		@Override
		protected String doInBackground(String... params) {
			User user = new User(activity);
			Map<String,String> map = user.getUserInfo(
					this.getTextValue(R.id.txtActivationCode));
			String userPhone = "";
			if(!map.isEmpty()) {
				userPhone = map.get("phoneNumber");
			}
			
			HttpHelper httpHelper = new HttpHelper();
			httpHelper.setPost(true);
			httpHelper.setUrl(params[0]);
			httpHelper.setPostParam("appId", "NPAPP");
			httpHelper.setPostParam("req", "ACT");
			httpHelper.setPostParam("user", userPhone);
			httpHelper.setPostParam("akey", 
					this.getTextValue(R.id.txtActivationCode));
			httpHelper.setPostParam("gotoApp", "WBTOM");
			httpHelper.setPostParam("gotoReq", "VIEW");
			httpHelper.setPostParam("dfmt", "JS");				
			httpHelper.sendRequest();
			this.setResponse(httpHelper.getResponse());
		
			return this.getResponse();
		}
		
	}
}
